package com.msmall.community.klg.article.action.html;

import com.msmall.base.action.BaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.model.SegmentArticle;
import com.msmall.community.klg.service.ArticleManager;
public class ArticleAction extends BaseAction{

	public static final String SESSION_ARTICLE_KEY = "SESSION_ARTICLE_KEY";
	
	private static final long serialVersionUID = 3721517834474862349L;
	
	private Long parentType;
	private Long valueType;
	
	private Long articleid;
	
	private Long topSegmentid;
	
	private Long segid;
	private CascadeCollection<Segment> current;
	
	private Article article;
	
	private ArticleManager articleManager;

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	
	/**
	 * viewArticle查看文章
	 */
	public String execute(){
		try {
			initTopSegment();
			article = articleManager.getArticle(articleid);
			page.setPageSize(30);
			articleManager.getArticleReplyPaginationByArticle(page, getUserid(), articleid);
			page.setUrl(ConstantUrl.HTML_KLG_ARTICLE_REPLY_PAGINATION + "?articleid="+ articleid);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}

	/**
	 * 进入创建帖子页面
	 * @return
	 */
	public String create(){
		initTopSegment();
		if(article == null){
			article = (Article)getFromSession(SESSION_ARTICLE_KEY);
		}
		if(message == null){
			message = (String) getFromSession(SESSION_MESSAGE_KEY);
			if(message!= null && getText("common.authentication.anonymous").equalsIgnoreCase(message)){
				if(getIsLogin()){
					message = null;
					removeFromSession(SESSION_MESSAGE_KEY);
				}
			}
		}
		return SUCCESS;
	}
	/**
	 * 保存帖子，用于创建和修改
	 * @return
	 */
	public String save(){
		try{
			initTopSegment();
			if(!getIsLogin()){
				message = getText("common.authentication.anonymous");
				setToSession(SESSION_ARTICLE_KEY, article);
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			
			int validtitle = articleManager.validateArticleTitle(article.getTitle());
			if(validtitle == 2){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.length.short");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validtitle == 3){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.length.long");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validtitle == 4){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.illegal");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			int validcontent = articleManager.validateArticleContent(article.getContent());
			if(validcontent == 2){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.length.short");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validcontent == 3){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.length.long");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validcontent == 4){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.illegal");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			article.setNickname(getNickname());
			article = articleManager.saveArticle(article, valueType, getUserid(), getUserAgentType());
			removeFromSession(SESSION_ARTICLE_KEY); //清空
			removeFromSession(SESSION_MESSAGE_KEY); //清空
			articleid = article.getArticleid();
		}catch(Exception e){
			log.debug("===========>", e);
		}
		return SUCCESS;
	}


	public void initTopSegment(){
		try{
			if(articleid !=null && articleid > 0L){
				SegmentArticle sa = articleManager.getSegmentByArticleid(articleid);
				segid = sa.getSegmentid();
			}
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> segment = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_NAV);
			if(topSegmentid == null || topSegmentid.compareTo(0L) ==0){
				if(segid == null || segid.compareTo(0L)==0){
					topSegmentid = segment.getChildren().get(0).getCurrent().getSegmentid();
					segid = segment.getChildren().get(0).getChildren().get(0).getCurrent().getSegmentid();
					current = cs.getAllDatas().get(segid);
				}else{
					current = cs.getAllDatas().get(segid);
					if(current.getFirstParent().getCurrent().getSegmentid().compareTo(ConstantDB.SEGMENT_ID_NAV) == 0){
						topSegmentid = current.getCurrent().getSegmentid();
					}else{
						topSegmentid = current.getFirstParent().getCurrent().getSegmentid();
					}
				}
			}
		}catch(Exception e){
			log.debug(e);
		}
	}
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Long getArticleid() {
		return articleid;
	}

	public void setArticleid(Long articleid) {
		this.articleid = articleid;
	}

	public Long getParentType() {
		return parentType;
	}

	public void setParentType(Long parentType) {
		this.parentType = parentType;
	}

	public Long getValueType() {
		return valueType;
	}

	public void setValueType(Long valueType) {
		this.valueType = valueType;
	}

	public Long getTopSegmentid() {
		return topSegmentid;
	}

	public void setTopSegmentid(Long topSegmentid) {
		this.topSegmentid = topSegmentid;
	}

	public Long getSegid() {
		return segid;
	}

	public void setSegid(Long segid) {
		this.segid = segid;
	}

	public CascadeCollection<Segment> getCurrent() {
		return current;
	}

	public void setCurrent(CascadeCollection<Segment> current) {
		this.current = current;
	}
}
